import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;


public class ChatClient {

  //Check Check
	
	private Socket sock;
	private PrintWriter pw;
	private BufferedReader keyboard;
	private Scanner scan;
	private String ip;
	private int port;
	
	ChatClient()
	{
		scan = new Scanner(System.in);
		keyboard = new BufferedReader(new InputStreamReader(System.in));
		
		System.out.print("¾ÆÀÌÇÇ¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä : ");
		ip = scan.next();//»ç¿ëÀÚ·ÎºÎÅÍ ¾ÆÀÌÇÇ¸¦ ÀÔ·Â¹ÞÀ½.
		System.out.println("");
		System.out.print("Æ÷Æ®¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä : ");
		port = scan.nextInt();
		
		try{
			
			sock = new Socket(ip,port);
			pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
			
			pw.println("Dawoon");
			pw.flush();
			
	
			
			//Ã¤ÆÃ ½º·¹µå°¡ ÀÛ¼ºµÇ¾î¾ß ÇÔ.
			ChatThread ct = new ChatThread(sock);
			ct.start();
			
			//»ç¿ëÀÚ°¡ ¼­¹ö·Î ³»¿ëÀ» Àü¼ÛÇÏ´Â ºÎºÐ.
			String word = "";
	
			while((word = keyboard.readLine()) != null)
			{	
				System.out.println("");
				pw.println(word);
				pw.flush();
				System.out.print("Àü¼ÛÇÒ ³»¿ë : ");
			}
		
		}catch(Exception e){System.out.println(e);}
		
	}
	
	class ChatThread extends Thread
	{
		private BufferedReader br;
		private String line;
		private Socket sock;
		
		ChatThread(Socket sock)
		{
			this.sock = sock;
		}
		
		public void run()//»ç¿ëÀÚ°¡ ÀÔ·ÂÇÑ ³»¿ëÀ» ÀÐ¾îµéÀÎ´Ù.
		{
			try{
				
				br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
				
				while( (line = br.readLine()) != null)
				{
					System.out.println(line);
				}
				
				br.close(); br=null;
				
			}catch(Exception e){System.out.println(e);}
		}//end of run
		
		
		
	}//end of thread
	
	
	public static void main(String[] args) {
		
		new ChatClient();
		
	}
	
}
